package com.console.operations.loanmgr;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.common.test.GetDataFromImportFile;
import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.common.util.GetTempleteFileUtil;
import com.console.pages.LinkPage;
import com.console.pages.loanmgr.LoanCheckPage;
/**
 * author: wyq 
 * 2016/9/1
 * 后台-借款管理-借款审核
 */
//借款审核操作
public class LoanCheck extends OperationBase {
	
	//借款是否存在于审核列表中
	public boolean isLoanExistInAudit = false;
	//审核后的状态
	public boolean isstatuscorrected = false;

	public LoanCheck(UITestBase tb) {
		super(tb);
		// TODO Auto-generated constructor stub
	}
	
	public void clickLink() throws Exception{
		switchToDefaultContent();
		LinkPage lp = new LinkPage(tb);
		//直到“借款管理”链接可见，超时就报错
		waitForElementDisplayed(lp.loanManageLnk);
		//点击“借款管理”链接
		lp.loanManageLnk.click();
		//直到“借款审核”链接存在，超时就报错
		waitForElementExist(lp.loanAuditLnk);
		//防止页面刷新，到刷新完后该控件才可点击
		elementToBeClickable(lp.loanAuditLnk);
		//点击“借款审核”链接
		lp.loanAuditLnk.click();
		Thread.sleep(1000);
		lp.loanAuditLnk.click();
		Thread.sleep(1000);
		
	}
	public void checkLoan() throws Exception{
		switchToDefaultContent();
		LoanCheckPage lc = new LoanCheckPage(tb);
		waitForElementDisplayed(lc.loanManage);
		String loanTitle = GetDataFromImportFile.getOneLineDataFromCell(GetTempleteFileUtil.PUBLOANBATCH_TEMPLATE, "借款标题");
		//动态设置DefaultElement的findValue
		lc.checkLink.setfindValue("//tr[td[text()='"+ loanTitle +"']]/td[a[text()='审核']]/a");
		waitForElementExist(lc.pagination);
		List<WebElement> subelement = lc.pagination.getNativeElement().findElements(By.tagName("li"));
		int pagecount = Integer.parseInt(subelement.get(subelement.size()-1).getText());
		log.info("page count is " + pagecount);
		System.out.println("page count is " + pagecount);
		//分页处理，直到找到相应控件为止
		
		if(pagecount>1){
			for(int i=1;i<pagecount;i++){
				if(isPageContainsStr(loanTitle)){
					waitForElementDisplayed(lc.checkLink);
					lc.checkLink.click();
					isLoanExistInAudit = true;
					break;
				}else{
					lc.pageLink.setfindValue("//li[a[text()='"+ (i+1) +"']]/a");
					lc.pageLink.click();
					if(i!=pagecount-1){
						lc.pageLink.setfindValue("//li[a[text()='"+ (i+2) +"']]/a");
						waitForElementExist(lc.pageLink);
					}
				}
			}
		}else{
			if(isPageContainsStr(loanTitle)){
				System.out.println(loanTitle);
				isLoanExistInAudit = true;
				waitForElementDisplayed(lc.checkLink);
				lc.checkLink.click();
			}
		}
		if(!isLoanExistInAudit){
			log.error("审核里找不到该借款 \"" + loanTitle + "\"");
			System.out.println("审核里找不到该借款 \"" + loanTitle + "\"");
			return;
		}
		waitForElementDisplayed(lc.platformSelect);
		String platform = GetDataFromImportFile.getOneLineDataFromCell(GetTempleteFileUtil.INVESTBATCH_TEMPLATE, "发布平台");
		lc.platformSelect.selectOptionByText(platform);
		//点击通过按钮
		lc.passButton.click();
		waitForElementDisplayed(lc.confirmButton);
		//点击弹出框上的确认按钮
		lc.confirmButton.click();
		waitForPageContainsStr("审批通过");
		String dcstatus = DBUtil.queryOneCell("select dc_status from fiz_plan where vc_name = ?", new Object[]{loanTitle});
		isstatuscorrected = dcstatus.equals("10");
		if(!isstatuscorrected){
			log.error("the status is not 招标中!");
			return;
		}
		
		
		
	}

}
